package com.example.moblie2021606143cwj0408;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private TextView tvMainTitle;
    private CheckBox checkboxMainYinqing;
    private CheckBox checkBoxMainBoli;
    private CheckBox checkBoxMainFangxiangpan;
    private Button btnMainBuy;
    private TextView tvMainTip;
    private TextView tvMainResult;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        checkboxMainYinqing.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    tvMainResult.setText("选中"+buttonView.getText().toString());
                }else{
                    tvMainResult.setText("取消选中"+buttonView.getText().toString());
                }
            }

        });
        checkBoxMainBoli.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    tvMainResult.setText("选中"+buttonView.getText().toString());
                }else{
                    tvMainResult.setText("取消选中"+buttonView.getText().toString());
                }
            }

        });
       checkBoxMainFangxiangpan.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    tvMainResult.setText("选中"+buttonView.getText().toString());
                }else{
                    tvMainResult.setText("取消选中"+buttonView.getText().toString());
                }
            }
        });
       btnMainBuy.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               String msg="选中：";
               if (checkboxMainYinqing.isChecked()){
                   msg+="\n"+checkboxMainYinqing.getText().toString();
               }
               if (checkBoxMainBoli.isChecked()) {
                   msg += "\n" + checkBoxMainBoli.getText().toString();
               }
               if (checkBoxMainFangxiangpan.isChecked()) {
                   msg += "\n" + checkBoxMainFangxiangpan.getText().toString();
               }
               tvMainResult.setText(msg);
           }
       });

    }
    private void initView() {
        tvMainTitle = (TextView) findViewById(R.id.tv_main_title);
        checkboxMainYinqing = (CheckBox) findViewById(R.id.checkbox_main_yinqing);
        checkBoxMainBoli = (CheckBox) findViewById(R.id.checkBox_main_boli);
        checkBoxMainFangxiangpan = (CheckBox) findViewById(R.id.checkBox_main_fangxiangpan);
        btnMainBuy = (Button) findViewById(R.id.btn_main_buy);
        tvMainTip = (TextView) findViewById(R.id.tv_main_tip);
        tvMainResult = (TextView) findViewById(R.id.tv_main_result);
    }
}